package main

import "fmt"

/**
Given an array and a value, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.

two pointer

给出一个数组以及一个目标值，移除数组中所有目标值返回所有其他值的个数 注:不能借助其他数组或空间

分析: 这道题跟remove duplicates from sorted array很像

看到不能借助其他数组或空间就想到了使用快慢指针
*/

func removeElement(nums []int, val int) int {
	i, j := 0, 0 //i快指针 j慢指针
	for ; i < len(nums); i++ {
		if nums[i] != val {
			nums[j] = nums[i]
			j++ //j++在赋值之后 这点与26.不同
		}
	}

	return j
}

func main() {
	length := removeElement([]int{1, 2, 3, 3}, 3)
	fmt.Print(length)
}
